package com.sxpi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.common.result.Result;
import com.sxpi.mapper.LikesMapper;
import com.sxpi.model.entity.Likes;
import com.sxpi.service.LikesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements LikesService {

    @Override
    public void removeLike(Long userId, String workId, String costumesId) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getUserId, userId);
        if (workId != null) {
            queryWrapper.eq(Likes::getWorkId, Long.parseLong(workId));
        }
        if (costumesId != null) {
            queryWrapper.eq(Likes::getCostumesId, Long.parseLong(costumesId));
        }
        this.remove(queryWrapper);
    }

    @Override
    public Long getLikeCountByWorkId(Long workId) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getWorkId, workId);
        return this.count(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Map<String, Object>> handleLike(Long userId, Integer isLike, Map<String, String> likeData) {
        Map<String, Object> resultMap = new HashMap<>();
        String workId = likeData.get("workId");
        String costumesId = likeData.get("costumesId");

        try {
            // 参数校验
            if (userId == null) {
                return Result.build(null,400,"用户ID不能为空");
            }
            if (workId == null && costumesId == null) {
                return Result.build(null,400,"workId和costumesId不能同时为空");
//                return Result.error("workId和costumesId不能同时为空");
            }

            if (isLike == 1) {
                // 检查是否已经点赞
                if (checkLikeExists(userId, workId, costumesId)) {
                return Result.build(null,400,"已经点赞过了");
//                    return Result.error("已经点赞过了");
                }
                // 点赞操作
                Likes like = new Likes();
                like.setUserId(userId);
                if (workId != null) {
                    like.setWorkId(Long.parseLong(workId));
                }
                if (costumesId != null) {
                    like.setCostumesId(Long.parseLong(costumesId));
                }
                this.save(like);
                resultMap.put("message", "点赞成功");
            } else if (isLike == 0) {
                // 取消点赞操作
                removeLike(userId, workId, costumesId);
                resultMap.put("message", "取消点赞成功");
            } else {
                return Result.build(null,400,"已经点赞过了");
//                return Result.error("无效的操作");
            }

            // 如果传入了 workId，查询并返回点赞数量
            if (workId != null) {
                Long likeCount = getLikeCountByWorkId(Long.parseLong(workId));
                resultMap.put("likeCount", likeCount);
            }

            return Result.ok(resultMap);
        } catch (NumberFormatException e) {
            log.error("参数格式转换错误", e);
                return Result.build(null,400,"参数格式错误");
//            return Result.error("参数格式错误");
        } catch (Exception e) {
            log.error("处理点赞操作时发生错误", e);
                return Result.build(null,400,"操作失败");
//            return Result.error("操作失败");
        }
    }

    /**
     * 检查是否已经点赞
     */
    private boolean checkLikeExists(Long userId, String workId, String costumesId) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getUserId, userId);
        if (workId != null) {
            queryWrapper.eq(Likes::getWorkId, Long.parseLong(workId));
        }
        if (costumesId != null) {
            queryWrapper.eq(Likes::getCostumesId, Long.parseLong(costumesId));
        }
        return this.count(queryWrapper) > 0;
    }
} 